from alibabacloud_chatbot20171011 import models as chatbot_20171011_models

from django.conf import settings

from core.base import AliClient


class HttpRequest:
    client = AliClient()

    def execute(self, execute_name, request):
        _execute = getattr(self.client, execute_name)
        response = _execute(request)
        return response.body


class CategoryRequest(HttpRequest):
    def get_list(self, parent_category_id, show_childrens):
        execute_name = 'query_categories'
        request = chatbot_20171011_models.QueryCategoriesRequest()
        if parent_category_id:
            if isinstance(parent_category_id, int):
                parent_category_id = str(parent_category_id)
            if not isinstance(parent_category_id, str) or parent_category_id.isdigit():
                return
            request.parent_category_id = parent_category_id
        if show_childrens:
            request.show_childrens = show_childrens
        return self.execute(execute_name=execute_name, request=request)

    def get_describe(self, category_id):
        execute_name = 'describe_category'
        request = chatbot_20171011_models.DescribeCategoryRequest()
        request.category_id = category_id

        return self.execute(execute_name=execute_name, request=request)

    def create(self, name, parent_category_id, knowledge_type, biz_code):
        execute_name = 'create_category'
        request = chatbot_20171011_models.CreateCategoryRequest()
        if not name:
            return
        if parent_category_id:
            if isinstance(parent_category_id, int):
                parent_category_id = str(parent_category_id)
            if not isinstance(parent_category_id, str) or parent_category_id.isdigit():
                return
            request.parent_category_id = request.parent_category_id
        request.name = name
        return self.execute(execute_name=execute_name, request=request)


class KnowledgeRequest(HttpRequest):
    def create(self):
        pass

    def get_describe(self, knowledge_id) -> chatbot_20171011_models.DescribeKnowledgeResponseBody:
        execute_name = 'describe_knowledge'
        request = chatbot_20171011_models.DescribeKnowledgeRequest
        request.knowledge_id = knowledge_id
        return self.execute(execute_name=execute_name, request=request)


class ChatRequest(HttpRequest):
    def get_answer(self, utterance) -> chatbot_20171011_models.ChatResponseBody:
        execute_name = 'chat'
        request = chatbot_20171011_models.ChatRequest()
        request.instance_id = settings.TEST_ROBOT_ID
        request.utterance = utterance
        return self.execute(execute_name=execute_name, request=request)

    def get_knowledge_describe(self, knowledge_id):
        return KnowledgeRequest().get_describe(knowledge_id=knowledge_id)
